package com.hex.ds.hdtp.core.inf.data.contrast.repository.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.data.contrast.mapper.ICrossDbContrastRunLogMapper;
import com.hex.ds.hdtp.core.inf.data.contrast.po.CrossDbContrastRunLogPo;
import com.hex.ds.hdtp.core.inf.data.contrast.repository.ICrossDbContrastRunLogRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.inf.data.contrast.repository.impl
 * @ClassName CrossDbContrastRunLogRepository
 * @Description: 跨库比对记录-数据接口实现
 * @Author: wz.li
 * @Date 2023/7/28 17:43
 * @Version v2.0.2
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastRunLogRepository implements ICrossDbContrastRunLogRepository {

    private final ICrossDbContrastRunLogMapper crossDbContrastRunLogMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastRunLogPo, toPage] <br>
     * @Return: com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.data.contrast.po.CrossDbContrastRunLogPo> <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:45 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Page<CrossDbContrastRunLogPo> queryListByPage(CrossDbContrastRunLogPo crossDbContrastRunLogPo, Page toPage) {
        final IPage tmpPageResult = crossDbContrastRunLogMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(toPage)
                , crossDbContrastRunLogPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    @Override
    public List<CrossDbContrastRunLogPo> queryByTaskIdStatus(String taskPkId, String status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("task_pk_id", taskPkId);
        queryWrapper.eq("status", status);
        return crossDbContrastRunLogMapper.selectList(queryWrapper);
    }

    /**
     * @Method modifySuccessNum
     * @Param
     * @param pkId
     * @Return void
     * @Description 根据主键更新成功数量
     * @Author Wang zhihao
     * @Date 2023/8/1 10:17
     * @Version V1.0
     */
    @Override
    public void modifySuccessNum(String pkId) {
        crossDbContrastRunLogMapper.modifySuccessNum(pkId);
    }

    /**
     * @Method modifyFailNum
     * @Param
     * @param pkId
     * @Return void
     * @Description 根据主键更新失败数量
     * @Author Wang zhihao
     * @Date 2023/8/1 10:18
     * @Version V1.0
     */
    @Override
    public void modifyFailNum(String pkId) {
        crossDbContrastRunLogMapper.modifyFailNum(pkId);
    }

    /**
     * @Method modifyStatus
     * @Param
     * @param pkId
     * @Return void
     * @Description 根据主键更新状态
     * @Author Wang zhihao
     * @Date 2023/8/1 10:18
     * @Version V1.0
     */
    @Override
    public void modifyStatus(String pkId) {
        crossDbContrastRunLogMapper.modifyStatus(pkId);
        crossDbContrastRunLogMapper.modifyFinishTime(pkId, DateUtil.now().toString());
    }

    /**
     * @Method modifyStopTask
     * @Param pkId
     * @Param status
     * @Return java.lang.Integer
     * @Description 停止比对任务
     * @Author gj.xu
     * @Date 2024/1/11 20:36
     * @Version V2.0
     */
    @Override
    public Integer modifyStopTask(String pkId, String status) {
        return crossDbContrastRunLogMapper.modifyStopTask(pkId, status, DateUtil.now());
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: com.hex.ds.hdtp.core.inf.data.contrast.po.CrossDbContrastRunLogPo <br>
     * @Description：根据主键查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:46 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public CrossDbContrastRunLogPo queryById(String pkId) {
        return crossDbContrastRunLogMapper.selectById(pkId);
    }

    /*
     * @Method: add <br>
     * @Param: [crossDbContrastRunLogPo] <br>
     * @Return: java.lang.Integer <br>
     * @Description：新建<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:46 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer add(CrossDbContrastRunLogPo crossDbContrastRunLogPo) {
        return crossDbContrastRunLogMapper.insert(crossDbContrastRunLogPo);
    }

    /*
     * @Method: modifyById <br>
     * @Param: [crossDbContrastRunLogPo] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据主键更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:46 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer modifyById(CrossDbContrastRunLogPo crossDbContrastRunLogPo) {
        return crossDbContrastRunLogMapper.updateById(crossDbContrastRunLogPo);
    }

    /*
     * @Method: removeById <br>
     * @Param: [pkId] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:47 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer removeById(String pkId) {
        return crossDbContrastRunLogMapper.deleteById(pkId);
    }

    public CrossDbContrastRunLogPo getById(String logId){
        return crossDbContrastRunLogMapper.selectById(logId);
    }
    /*
     * @param pkId:
     * @return String
     * @author Liang Fuhao
     * @description 根据id查状态
     * @date 2024/1/15 11:03
     */
    public String queryStatusByPkId(String pkId){
        return crossDbContrastRunLogMapper.selectById(pkId).getStatus();
    }
}
